# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=lua-busted
_pkgname=busted
pkgver=2.1.2
pkgrel=0
pkgdesc="Elegant Lua unit testing"
url="https://lunarmodules.github.io/busted/"
arch="noarch"
license="MIT"
# Note: We have replaced dkjson with lua-cjson.
depends="lua-cliargs lua-filesystem lua-system lua-cjson lua-say
	lua-luassert lua-term lua-penlight lua-mediator"
checkdepends="luajit"
source="$pkgname-$pkgver.tar.gz::https://github.com/lunarmodules/busted/archive/v$pkgver.tar.gz
	use-cjson.patch"
builddir="$srcdir/$_pkgname-$pkgver"
# tests fail on x86_64 because cl_standalone.lua output is different, i don't know why --kaniini
options="!check"

_luaversions="5.1 5.2 5.3 5.4"
for _v in $_luaversions; do
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
	checkdepends="$checkdepends lua$_v"
done

prepare() {
	default_prepare

	local lver; for lver in $_luaversions jit; do
		sed "s|/usr/bin/env lua|/usr/bin/lua$lver|" \
			bin/busted > bin/busted-$lver
	done
}

check() {
	mv bin/busted bin/busted.orig

	local lver; for lver in $_luaversions jit; do
		# Some specs invokes bin/busted as subprocess, so we must
		# ensure that it will run on the correct Lua version.
		install -m 755 bin/busted-$lver bin/busted

		msg "Running tests on lua$lver..."
		lua$lver bin/busted spec
	done

	mv bin/busted.orig bin/busted
}

package() {
	# Executable with shebang #!/usr/bin/env lua.
	install -m 755 -D bin/busted "$pkgdir"/usr/bin/busted
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="lua$lver ${depends//lua-/lua$lver-}"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"
	local destdir="$subpkgdir/usr/share/lua/$lver/$_pkgname"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$_pkgname/$pkgver-1"

	cd "$builddir"

	mkdir -p "$destdir"
	cp -r busted/* "$destdir"

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest

	install -m 755 -D bin/busted-$lver "$subpkgdir"/usr/bin/busted-$lver
}

sha512sums="
87aa9bf36c946b22940b2f3361d66fc59af7042deaa695c576f80602f825d5c9334b91edce4fb297098deb3edb574a10aebfd552866f039a1fb640978e022f18  lua-busted-2.1.2.tar.gz
8495713a5710d1b4369c992061ca5c424577c7cd487c73e9b045c7e4eb2a417b87955a5bd41b7ceb20cfb77f3331443b4bfeec179af973883dacae47f6988a2a  use-cjson.patch
"
